package edu.iyte.ceng316.ui.phonebook.dialog;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import java.awt.Insets;
import java.util.List;


import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;

import edu.iyte.ceng316.model.PhoneGroup;

public class DepartmentPanel extends JPanel {
	JLabel departmentLabel;
	JComboBox comboBox;
	
	private List<String> departmentNames;
	private PhoneGroup group;
	/**
	 * Create the panel.
	 */
	public DepartmentPanel(List<String> departmentNames,PhoneGroup group) {
		this.departmentNames = departmentNames;
		this.group=group;
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		departmentLabel = new JLabel("Name:");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
		gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		add(departmentLabel, gbc_lblNewLabel);
		
		comboBox = new JComboBox();
		GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
		gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_1.gridx = 1;
		gbc_comboBox_1.gridy = 0;
		add(comboBox, gbc_comboBox_1);
		initDataBindings();
	}

	protected void initDataBindings() {
		JComboBoxBinding<String, List<String>, JComboBox> jComboBinding = SwingBindings.createJComboBoxBinding(UpdateStrategy.READ, departmentNames, comboBox);
		jComboBinding.bind();
		//
		BeanProperty<PhoneGroup, String> phoneGroupBeanProperty = BeanProperty.create("name");
		BeanProperty<JComboBox, String> jComboBoxBeanProperty = BeanProperty.create("selectedItem");
		AutoBinding<PhoneGroup, String, JComboBox, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, group, phoneGroupBeanProperty, comboBox, jComboBoxBeanProperty);
		autoBinding.bind();
	}
}
